package com.zhiyou100.crm.servlet.user;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zhiyou100.crm.service.UserService;
import com.zhiyou100.crm.service.impl.UserServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.User;

/**
 * 删除用户
 */
@WebServlet("/user/remove")
public class RemoveServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	UserService userService=new UserServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		//选择ID
		int userId = Integer.parseInt(request.getParameter("id"));
		//登录用户名id
		User user = (User)request.getSession().getAttribute("user");
		List<User> users = userService.queryAllUser();
		for (int i = 0; i < users.size(); i++) {
			if(users.get(i).getUser_id()==user.getUser_id()) {
				if(users.get(i).getIs_admin()==0){
					response.getWriter().print("<script>alert('您没有权限！');window.history.go(-1);</script>");
				}else {
					boolean b = userService.deleteUser(userId);
					if (b) {
						response.sendRedirect(request.getContextPath() + "/user/list");
					}
				}
			}

		}

	}

}
